﻿using System;
using System.Data;
using MedicService.Domain;
using MedicService.Server.Factory.Generic;

namespace MedicService.Server.Factory {
    public class HistoryFactory : EntityFactory<History> {
        public HistoryFactory() : base("History") {}

        protected override History CreateEntity() {
            return new History();
        }

        protected override string GetInsertQuery(History entity) {
            string q = "INSERT INTO [History]"
                       + " ([userId]"
                       + " ,[date])"
                       + " VALUES"
                       + " (" + entity.User.Id
                       + " ," + DateTime(entity.Date) + ");"
                       + " ";
            return q;
        }

        protected override string GetUpdateQuery(History entity) {
            string q = "UPDATE [History]"
                       + " SET [userId] = " + entity.User.Id
                       + " ,[date] = " + DateTime(entity.Date)
                       + " WHERE id=" + entity.Id;
            return q;
        }

        protected override void FillEntity(History entity, DataRow myDataRow) {
            base.FillEntity(entity, myDataRow);
            entity.User = new UserFactory().Load(myDataRow["userId"]);
            entity.Date = (DateTime) myDataRow["date"];
        }
    }
}